public class Main {
    public int search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;

        if (nums[left] == target) {
            return left;
        }
        if (nums[right] == target) {
            return right;
        }

        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid; // 找到目标值，返回索引
            } else if (nums[mid] > target) {
                right = mid - 1; // 调整右边界
            } else {
                left = mid + 1; // 调整左边界
            }
        }
        return -1;
    }
}
